package com.hzhc.utils;

import org.apache.pdfbox.multipdf.Splitter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
import java.util.ListIterator;


/**
 * @author wuxiaoke
 * @description TODO
 * @date 2024/8/14 上午10:45
 */
public class PDFUtils {

    public static String splitPdf(int pageNum, InputStream inputStream, String dest) throws IOException {
//        File indexFile = new File(source);
        File outFile = new File(dest);
        PDDocument document = null;
        try {
//            document = PDDocument.load(indexFile);
            document = PDDocument.load(inputStream);
            if (pageNum > document.getNumberOfPages()) {
                return "页数超出范围";
            }
            Splitter splitter = new Splitter();
            splitter.setStartPage(pageNum);
            splitter.setEndPage(pageNum);
            List<PDDocument> documents = splitter.split(document);
            ListIterator<PDDocument> iterator = documents.listIterator();
            while (iterator.hasNext()) {
                PDDocument pd = iterator.next();
                if(outFile.exists()){
                    outFile.delete();
                }
                pd.save(outFile);
                pd.close();
                if(outFile.exists()){
                    return outFile.getPath();
                }
            }
            document.close();
        }catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean pdfFileToImage(File pdffile,String targetPath){
        try{
            FileInputStream inputStream = new FileInputStream(pdffile);
            InputStream byteInputStream = null;
            PDDocument doc = PDDocument.load(inputStream);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            if(pageCount > 0){
                BufferedImage image = renderer.renderImage(0,4.0f);
                image.flush();
                ByteArrayOutputStream bs = new ByteArrayOutputStream();
                ImageOutputStream imOut;
                imOut = ImageIO.createImageOutputStream(bs);
                ImageIO.write(image, "png", imOut);
                byteInputStream = new ByteArrayInputStream(bs.toByteArray());
                byteInputStream.close();
            }
            doc.close();
            File uploadFile = new File(targetPath);
            FileOutputStream fops;
            fops = new FileOutputStream(uploadFile);
            if (byteInputStream != null) {
                fops.write(readInputStream(byteInputStream));
            }
            fops.flush();
            fops.close();
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public static byte[] readInputStream(InputStream inputStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(buffer))!= -1) {
            outStream.write(buffer, 0, len);
        }
        outStream.close();
        inputStream.close();
        return outStream.toByteArray();
    }

//    public static void main(String[] args) {
//        try {
//            File file = new File("/Users/wuxiaoke/Downloads/a/6.17耐火材料10运单.pdf");
//            InputStream inputStream = new FileInputStream(file);
//            String path = splitPdf(4,inputStream , "/Users/wuxiaoke/Downloads/a/b/6.17耐火材料10运单.pdf");
//            File file1 = new File(path);
//            //上传的是png格式图片
//            String fileName = "/Users/wuxiaoke/Downloads/a";
//            pdfFileToImage(file1, fileName);
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//
//    }
}
